﻿Imports System.Net
Imports TNCBAL
Imports TNCDAL
Imports HTLibrary

Public Class ucForeignCompany
    Dim db As New TanNamChinhDataContext
    Dim m_fc As New CForeignCompany

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DataGridViewHelper.DataGridView_InitEvent(dtgFCompany)
    End Sub

    Private Sub ucForeignCompany_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        KhoiTaodtgFCompany()
        KhoiTao_GiaoDien(True)
    End Sub

    Public Sub KhoiTaodtgFCompany(Optional ByVal NameUpdate As String = "", Optional ByVal Name As String = "")
        Dim fc = m_fc.SelectAllRecordByName(Name)

        Dim source As New BindingSource
        source.DataSource = fc
        BindingNavigator1.BindingSource = source

        dtgFCompany.DataSource = source
        FormatdtgFcompany()

        Try
            If NameUpdate = "" Then
                dtgFCompany.Rows(0).Selected = True
                dtgFCompany.FirstDisplayedScrollingRowIndex = 0
            Else
                Dim index As Integer = 0
                For i As Integer = 0 To dtgFCompany.RowCount - 1
                    If dtgFCompany.Rows(i).Cells("Name").Value = NameUpdate Then
                        index = i
                        Exit For
                    End If
                Next

                dtgFCompany.Rows(index).Cells("Name").Selected = True
            End If
            'dtgFCompany.Rows(indexRow).Selected = True
            'dtgFCompany.FirstDisplayedScrollingRowIndex = indexRow
        Catch ex As Exception

        End Try
        
    End Sub

    Public Sub FormatdtgFcompany()
        'dtgFCompany.RowHeadersWidth = 20
        dtgFCompany.RowHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgFCompany.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

        dtgFCompany.Columns("Name").Frozen = True

        dtgFCompany.MultiSelect = False
        dtgFCompany.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgFCompany.ReadOnly = True
        dtgFCompany.BackgroundColor = Color.White
        dtgFCompany.AllowUserToAddRows = False
        dtgFCompany.AllowUserToDeleteRows = False

        dtgFCompany.Columns("ID_ForeignCompany").Visible = False
        dtgFCompany.Columns("IsActive").Visible = False
        With dtgFCompany.Columns("Name")
            .Width = 250
        End With
        'With dtgFcompany.Columns("TruckNumber")
        '    .HeaderText = "Biển Số"
        '    .Width = 100
        'End With
    End Sub

#Region "btn Them, Xoa, Sua"
    Dim f_Them As Boolean = False
    Dim f_Sua As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Clear()
        txtName.Focus()
        KhoiTao_GiaoDien(False)
        f_Them = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        KhoiTao_GiaoDien(False)
        txtName.Focus()
        f_Sua = True
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        Dim ID_ForeignCompany As Integer = dtgFCompany.CurrentRow.Cells("ID_ForeignCompany").Value
        'kiem tra truoc khi xoa
        Dim FCName As String = dtgFCompany.CurrentRow.Cells("Name").Value
        If (MsgBox("Bạn muốn xóa " + FCName + " ra khỏi danh sách", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes) Then
            Try
                m_fc.Delete(ID_ForeignCompany)
                KhoiTaodtgFCompany()
                'write systemlog
                Dim objLogController As New LogController
                objLogController.Insert(enumAction.Delete, String.Format("ForeignCompany Name {0}", FCName))
            Catch ex As Exception
                MsgBox("Del - Foreign Company: " + FCName + " đang được sử dụng. Không xóa được")
            End Try
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim obj_fc As New tblForeignCompany
        obj_fc.Address = txtAddress.Text
        obj_fc.Fax = txtFax.Text
        obj_fc.Name = txtName.Text
        obj_fc.Tel = txtTelephone.Text
        'obj_fc.IsExport = chkIsExport.Checked

        If f_Them Then
            obj_fc.IsActive = True
            m_fc.Insert(obj_fc)
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Add, String.Format("ForeignCompany Name {0}", obj_fc.Name))
            KhoiTaodtgFCompany(txtName.Text, txtNameSearch.Text.Trim)
        End If

        If f_Sua Then
            obj_fc.ID_ForeignCompany = dtgFCompany.CurrentRow.Cells("ID_ForeignCompany").Value
            m_fc.Update(obj_fc)

            KhoiTaodtgFCompany(txtName.Text, txtNameSearch.Text.Trim)

            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Edit, String.Format("ForeignCompany Name {0}", obj_fc.Name))
        End If

        f_Them = False
        f_Sua = False
        KhoiTao_GiaoDien(True)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        KhoiTao_GiaoDien(True)
        KhoiTaodtgFCompany(dtgFCompany.CurrentRow.Cells("Name").Value, txtNameSearch.Text)
    End Sub

    Public Sub KhoiTao_GiaoDien(ByVal flag As Boolean)
        btnAdd.Enabled = flag
        btnEdit.Enabled = flag
        btnDel.Enabled = flag
        btnUpdate.Enabled = Not flag
        btnCancel.Enabled = Not flag
        txtName.ReadOnly = flag
        txtAddress.ReadOnly = flag
        txtFax.ReadOnly = flag
        txtTelephone.ReadOnly = flag
        'chkIsExport.Enabled = Not flag
        dtgFCompany.Enabled = flag


        'If flag Then
        '    txtName.BackColor = Color.Azure
        '    txtAddress.BackColor = Color.Azure
        '    txtFax.BackColor = Color.Azure
        '    txtTelephone.BackColor = Color.Azure
        'Else
        '    txtName.BackColor = Color.White
        '    txtAddress.BackColor = Color.White
        '    txtFax.BackColor = Color.White
        '    txtTelephone.BackColor = Color.White
        'End If
    End Sub

    Public Sub Clear()
        txtName.Text = ""
        txtAddress.Text = ""
        txtFax.Text = ""
        txtTelephone.Text = ""
        'chkIsExport.Checked = False
    End Sub
#End Region

    Private Sub dtgFCompany_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgFCompany.CurrentCellChanged
        Try
            txtName.Text = dtgFCompany.CurrentRow.Cells("Name").Value
            txtAddress.Text = dtgFCompany.CurrentRow.Cells("Address").Value
            txtFax.Text = dtgFCompany.CurrentRow.Cells("Fax").Value
            txtTelephone.Text = dtgFCompany.CurrentRow.Cells("Tel").Value
            'chkIsExport.Checked = dtgFCompany.CurrentRow.Cells("IsExport").Value
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        KhoiTaodtgFCompany("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        txtNameSearch.Text = ""
        KhoiTaodtgFCompany("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub txtNameSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNameSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            KhoiTaodtgFCompany("", txtNameSearch.Text.Trim)
        End If
    End Sub
End Class
